package com.suray.wcs.res.msgparse;

import com.suray.basic.wcs.utils.Crc16;

/**
 * 发送命令对象
 */
public class CmdSendBeanV3 {
    private int index_w = 0;
    /**
     * 头帧
     */
    private int firstFrame = 0x02fd;
    /**
     * 小车编号
     */
    private int deviceNo;
    /**
     * 报文版本
     */
    private int version = 0x01;
    /**
     * 报文类型
     */
    private int frameType = 0x02;
    /**
     * 任务序号
     */
    private int taskNo;
    /**
     * 操作指令序号
     */
    private int cmdNo;
    /**
     * 指令ID
     */
    private int cmd;
    /**
     * 指令参数
     */
    private int cmdParam;

    /**
     * 报文长度
     */
    private int msgLen;
    /**
     * 校验位
     */
    private int crc16;
    /**
     * 尾帧
     */
    private int endFrame = 0x03fc;

    public CmdSendBeanV3(int deviceNo, int taskNo, int cmdNo, int cmd, int cmdParam) {
        this.deviceNo = deviceNo;
        this.taskNo = taskNo;
        this.cmdNo = cmdNo;
        this.cmd = cmd;
        this.cmdParam = cmdParam;
    }

    public CmdSendBeanV3(int deviceNo, int cmdNo, int cmd) {
        this.deviceNo = deviceNo;
        this.cmdNo = cmdNo;
        this.cmd = cmd;
    }

    public byte[] makeMsg() {
        byte[] bs = new byte[17];
        msgLen = bs.length;
        addToByte(bs, firstFrame, 2);
        addToByte(bs, deviceNo, 1);
        addToBytehalf(bs, version, frameType, 1);
        addToByte(bs, taskNo, 1);
        addToByte(bs, cmdNo, 1);
        addToByte(bs, cmd, 1);
        addToByte(bs, cmdParam, 4);
        addToByte(bs, msgLen, 2);
        crc16 = Crc16.getCRC16(bs);
        addToByte(bs, crc16, 2);
        addToByte(bs, endFrame, 2);
        return bs;
    }

    public CmdSendBeanV3(int deviceNo, int cmdNo, int cmd, int cmdParam) {
        this.deviceNo = deviceNo;
        this.cmdNo = cmdNo;
        this.cmd = cmd;
        this.cmdParam = cmdParam;
    }

    public int getIndex_w() {
        return index_w;
    }

    public void setIndex_w(int index_w) {
        this.index_w = index_w;
    }

    public int getFirstFrame() {
        return firstFrame;
    }

    public void setFirstFrame(int firstFrame) {
        this.firstFrame = firstFrame;
    }

    public int getVersion() {
        return version;
    }

    public void setVersion(int version) {
        this.version = version;
    }

    public int getFrameType() {
        return frameType;
    }

    public void setFrameType(int frameType) {
        this.frameType = frameType;
    }

    public int getMsgLen() {
        return msgLen;
    }

    public void setMsgLen(int msgLen) {
        this.msgLen = msgLen;
    }

    public int getCrc16() {
        return crc16;
    }

    public void setCrc16(int crc16) {
        this.crc16 = crc16;
    }

    public int getEndFrame() {
        return endFrame;
    }

    public void setEndFrame(int endFrame) {
        this.endFrame = endFrame;
    }


    public int getTaskNo() {
        return taskNo;
    }

    public void setTaskNo(int taskNo) {
        this.taskNo = taskNo;
    }

    public int getCmdNo() {
        return cmdNo;
    }

    public void setCmdNo(int cmdNo) {
        this.cmdNo = cmdNo;
    }

    public int getCmd() {
        return cmd;
    }

    public void setCmd(int cmd) {
        this.cmd = cmd;
    }

    public int getCmdParam() {
        return cmdParam;
    }

    public void setCmdParam(int cmdParam) {
        this.cmdParam = cmdParam;
    }

    byte[] addToByte(byte[] longBs, int value, int byteNum) {
        byte[] temp = MsgTransferUtil.addToByte(longBs, index_w, value, byteNum);
        index_w += byteNum;
        return temp;
    }

    byte[] addToBytehalf(byte[] longBs, int value1, int value2, int byteNum) {
        byte[] temp = MsgTransferUtil.addToBytehalf(longBs, index_w, value1, value2, byteNum);
        index_w += byteNum;
        return temp;
    }

    @Override
    public String toString() {
        return "CmdSendBeanV3{" +
                "index_w=" + index_w +
                ", firstFrame=" + firstFrame +
                ", version=" + version +
                ", frameType=" + frameType +
                ", taskNo=" + taskNo +
                ", cmdNo=" + cmdNo +
                ", cmd=" + cmd +
                ", cmdParam=" + cmdParam +
                ", msgLen=" + msgLen +
                ", crc16=" + crc16 +
                ", endFrame=" + endFrame +
                '}';
    }
}
